# -*- coding: utf-8 -*-

from datetime import datetime
from app import db
from app.base.tables import tb


# 会员
class Member(db.Model):
    __tablename__ = tb['common_member']
    member_id = db.Column(db.Integer, primary_key=True)
    pwd = db.Column(db.String(64))
    real_name = db.Column(db.String(32))
    email = db.Column(db.String(128), unique=True)
    phone = db.Column(db.String(11), unique=True)
    tel = db.Column(db.String(32))
    add_date = db.Column(db.DateTime, index=True, default=datetime.utcnow)
    add_member_id = db.Column(db.Integer)

    @staticmethod
    def add_member(pwd, real_name, email, phone, tel, add_date, add_member_id):
        mb = Member(pwd=pwd, real_name=real_name, email=email, phone=phone, tel=tel, add_date=add_date, add_member_id=add_member_id)
        db.session.add(mb)
        db.session.commit()
        return mb

    def get_member(self, w=3, search=None, fields=(), page=1, page_size=20, odb=None):
        """
        得到用户信息
            @:param int   w         1:计数, 2:数据, 3:计数+数据, 4:一条数据
            @:param dict  search    筛选条件 {"k1": "v1", "k2": "v2", "k3": "v3"}
            @:param tuple fields    选取字段 (Member.member_id, Member.real_name, ...)
            @:param int   page      页码 1
            @:param int   page_size 分页大小 50
            @:param odb             排序 Member.member_id.desc()
        """
        if search is None:
            search = {}

        # 初始化查询
        mb = self.query

        # 选取字段
        if len(fields) > 0:
            mb = mb.with_entities(*fields)

        # 筛选条件
        if 'member_id' in search:
            mb = mb.filter(Member.member_id == int(search['member_id']))
        if 'real_name' in search:
            mb = mb.filter(Member.real_name == search['real_name'])
        if 'email' in search:
            mb = mb.filter(Member.email == search['email'])

        # 分页
        page = int(page)
        if page < 1:
            page = 1
        page_size = int(page_size)

        # print(mb)

        # 返回条件
        w = int(w)
        if w == 1:
            return mb.count()
        elif w == 2:
            # 排序
            if odb is not None:
                mb = mb.order_by(odb)
            else:
                mb = mb.order_by(Member.member_id.desc())
            # 分页
            if page_size > 0:
                mb = mb.limit(page_size).offset((page - 1) * page_size)
            return mb.all()
        elif w == 3:
            ct = mb.count()
            # 排序
            if odb is not None:
                mb = mb.order_by(odb)
            else:
                mb = mb.order_by(Member.member_id.desc())
            # 分页
            if page_size > 0:
                mb = mb.limit(page_size).offset((page - 1) * page_size)
            dts = mb.all()
            return ct, dts
        elif w == 4:
            return mb.first()
        return None

    def __repr__(self):
        return "<Member %r>" % self.real_name


class MemberLog(db.Model):
    __tablename__ = tb['common_member_log']
    member_log_id = db.Column(db.Integer, primary_key=True)
    member_id = db.Column(db.Integer, default=0)
    ip = db.Column(db.String(64))
    add_date = db.Column(db.DateTime, default=datetime.utcnow)

    def init(self, member_id, ip, add_date):
        self.member_id = member_id
        self.ip = ip
        self.add_date = add_date

    def __repr__(self):
        return '<MemberLog %r>' % self.id
